package com.example.servicebase.exceptionhandler;

import com.example.commonutils.ResultData;
import com.example.servicebase.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
//@ControllerAdvice //统一异常返回信息
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody//为了能够返回数据
    public ResultData error(Exception e){
        log.error(ExceptionUtil.getMessage(e));
        return ResultData.error();
    }

    @ExceptionHandler(CustomException.class)
    @ResponseBody//为了能够返回数据
    public ResultData error(CustomException e){
        e.printStackTrace();
        return ResultData.error().code(e.getCode()).message(e.getMessage());
    }
}
